import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

class AutomaticKeepAlivePage extends StatefulWidget {
  @override
  _AutomaticKeepAlivePageState createState() => _AutomaticKeepAlivePageState();
}

class _AutomaticKeepAlivePageState extends State<AutomaticKeepAlivePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView(
        addRepaintBoundaries: true,
        addAutomaticKeepAlives: true,
        children: <Widget>[
          // AutomaticKeepAlive(child: Checkbox(value: true, onChanged: (bool value) {},),),
          Container(color: Colors.green,child: Text('ListView的addAutomaticKeepAlives: true时，children中的child会被嵌套再AutomaticKeeyalive中'),),
          KeeyAliveWidget(),
          Text('修改下面这个TextFormField的值，滚动到屏幕外再滚动回来,值不被保留'),
          TextFormField(initialValue: 'hello',),
          Container(
            height: 1000,
            color: Colors.red,
          ),
          TextFormField(initialValue: 'hello',),
          TextFormField(initialValue: 'hello',),
          TextFormField(initialValue: 'hello',),
          TextFormField(initialValue: 'hello',),
          TextFormField(initialValue: 'hello',),
          TextFormField(initialValue: 'hello',),
          TextFormField(initialValue: 'hello',),
          TextFormField(initialValue: 'hello',),
          TextFormField(initialValue: 'hello',),
          TextFormField(initialValue: 'hello',),
          TextFormField(initialValue: 'hello',),
          TextFormField(initialValue: 'hello',),
          TextFormField(initialValue: 'hello',),
          TextFormField(initialValue: 'hello',),
          TextFormField(initialValue: 'hello',),
          TextFormField(initialValue: 'hello',),
          TextFormField(initialValue: 'hello',),
          TextFormField(initialValue: 'hello',),
          TextFormField(initialValue: 'hello',),
          TextFormField(initialValue: 'hello',),
          TextFormField(initialValue: 'hello',),
          TextFormField(initialValue: 'hello',),
          TextFormField(initialValue: 'hello',),
          TextFormField(initialValue: 'hello',),
        ],
      ),
    );
  }
}

class KeeyAliveWidget extends StatefulWidget {
  @override
  KeeyAliveWidgetState createState() {
    return new KeeyAliveWidgetState();
  }
}

class KeeyAliveWidgetState extends State<KeeyAliveWidget> with AutomaticKeepAliveClientMixin {
  @override
  Widget build(BuildContext context) {
    return Column(children: <Widget>[
      Text('修改下面这个TextFormField的值，滚动到屏幕外再滚动回来可以保持之前的值'),
      TextFormField(initialValue: 'hello',),
    ],);
  }

  @override
  bool get wantKeepAlive => true;
}